<template>
  <div class="drawer_box">
    <div class="date_searchbox">
      <div>
        <el-date-picker
          style="width: 218px"
          v-model="value3"
          type="month"
          value-format="YYYY-MM"
          placeholder="选择月份"
          @change="chanse"
        />
      </div>
      <div class="month_box">
        <div
          @click="itemClick(index, item)"
          class="items flex_center_center"
          v-for="(item, index) in dateArr"
          :key="index"
          :class="{ isDate: item.is }"
        >
          {{ item.date }}
        </div>
      </div>
    </div>

    <div style="width: 80%" class="isTable">
      <div>
        <el-tabs v-model="activeName" type="card" @tab-click="handleClickTab">
          <el-tab-pane label="未处理" name="wcl"></el-tab-pane>
          <el-tab-pane label="已处理" name="ycl"></el-tab-pane>
        </el-tabs>
      </div>
      <div class="box_wcl isTable" v-if="activeName == 'wcl'">
      <el-table
        :data="ycqkList"
        fit
        highlight-current-row
        stripe
         :span-method="mergeCells"
        style="width: 100%"
        border
      height="680"
      >
      <el-table-column prop="zcfr" label="部门" align="center" width="120">
          <template #default="scope">
            {{ scope.row.orgName }}
          </template>
        </el-table-column>
        <el-table-column prop="startDate" label="时间" align="center" width="140">
          <template #default="scope">
            <div>
              {{
                scope.row.createDate.split("T")[0] +
                " " +
                scope.row.createDate.split("T")[1].split(":")[0] +
                ":" +
                scope.row.createDate.split("T")[1].split(":")[1]
              }}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="zcfr" label="上报人" align="center" width="120">
          <template #default="scope">
            {{ scope.row.reportUserName }}
          </template>
        </el-table-column>

        <el-table-column prop="zcfr" label="类型" align="center" width="200">
          <template #default="scope">
            {{ scope.row.exceptType }}
          </template>
        </el-table-column>

        <el-table-column prop="ld" label="状态" align="center" width="80">
          <template #default="scope">
     
            <div style="color: greenyellow " v-if="scope.row.state">
              已处理
            </div>
            <div style="color: red;" v-else>
              未处理
            </div>
            <!-- <el-button v-else text size="small" type="danger">未处理</el-button> -->
          </template>
        </el-table-column>
        <el-table-column  prop="ld" label="情况描述" align="center">
          <template #default="scope">
            {{ scope.row.content }}
          </template>
        </el-table-column>
      </el-table>
    </div>

    <div class="box_wcl isTable" v-if="activeName == 'ycl'">
      <el-table
        :data="ycqkList"
        fit
        highlight-current-row
        stripe
        
         :span-method="mergeCells"
        style="width: 100%"
        border
          height="680"
      >
          <el-table-column prop="zcfr" label="部门" align="center" width="120">
          <template #default="scope">
            {{ scope.row.orgName }}
          </template>
        </el-table-column>
        <el-table-column prop="startDate" label="时间" align="center"  width="160">
          <template #default="scope">
            <div>
              {{
                scope.row.createDate.split("T")[0] +
                " " +
                scope.row.createDate.split("T")[1].split(":")[0] +
                ":" +
                scope.row.createDate.split("T")[1].split(":")[1]
              }}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="zcfr" label="处理人" align="center" width="180">
          <template #default="scope">
            {{ scope.row.handleUserName }}
          </template>
        </el-table-column>

        <el-table-column prop="zcfr" label="类型" align="center" width="200">
          <template #default="scope">
            {{ scope.row.exceptType }}
          </template>
        </el-table-column>

        <el-table-column prop="ld" label="状态" align="center" width="80">
          <template #default="scope">
            <div style="color: green " v-if="scope.row.state">
              已处理
            </div>
            <div style="color: red;" v-else>
              未处理
            </div>
          </template>
        </el-table-column>
        <el-table-column  prop="ld" label="处理信息" align="center">
          <template #default="scope">
            {{ scope.row.handleContent }}
          </template>
        </el-table-column>
      </el-table>
    </div>
    </div>
  </div>
</template>

<script setup>
import {
  Document,
  Menu as IconMenu,
  Location,
  Setting,
  Back,
  Right,
} from "@element-plus/icons-vue";

import http from "@/utils/api";
import jjbjlTwo from "../components/jjbjlTwo.vue";
import towZfxx from "../components/towZfxx.vue";
import zdzf from "../components/zdzf.vue";
import yxry from "../components/yxry.vue";
import router from "@/router";
import jgsb from "@/components/jgsb.vue";
import { ElMessage, ElMessageBox } from "element-plus";
import {
  ref,
  watch,
  onMounted,
  onBeforeUnmount,
  reactive,
  onBeforeMount,
  nextTick,
  computed
} from "vue";
import { defineProps, defineEmits } from "vue";
let activeName = ref("wcl");
const props = defineProps({
  sonData: {
    type: String,
    default: "",
  },
  jqArr: {
    type: Array,
    default: [],
  },
  title: {
    type: String,
    default: "",
  },
});
let dateSfm = ref("");
let hour = ref("");
let daydata = ref("");
let datePicker = ref("");

let value3 = ref("");
let years = ref("");
let months = ref("");
let dateArr = ref([]);

const getFormattedDateTime = () => {
  const now = new Date();
  const year = now.getFullYear();
  const month = String(now.getMonth() + 1).padStart(2, "0");
  const day = String(now.getDate()).padStart(2, "0");
  const hours = String(now.getHours()).padStart(2, "0");
  const minutes = String(now.getMinutes()).padStart(2, "0");
  const seconds = String(now.getSeconds()).padStart(2, "0");
  dateSfm.value = `${hours}:${minutes}:${seconds}`;
  hour.value = String(now.getHours()).padStart(2, "0");
  daydata.value = `${year}-${month}-${day}`;
  years.value = `${year}`;
  months.value = `${month}`;
  return `${year}-${month}-${day} ${hour.value}`;
};


let isIndex = ref(0);


const chanse = (row) => {
  if (row) {
    years.value = `${row.split("-")[0]}`;
    months.value = `${row.split("-")[1]}`;
    getMonthDates();
  } else {
  }
};

const getMonthDates = () => {
  const now = new Date();
  const year = Number(years.value);
  const month = Number(months.value); // 注意：月份从0开始，所以11表示12月
  // 获取当前月的天数
  const daysInMonth = new Date(year, month, 0).getDate();
  // 生成日期数组
  const dates = [];
  for (let day = 1; day <= daysInMonth; day++) {
    const dateString = `${year}-${String(month).padStart(2, "0")}-${String(
      day
    ).padStart(2, "0")}`;
    dates.push({
      date: dateString,
      is: false,
    });
  }

  dateArr.value = dates;
  dateArr.value.forEach((item) => {
    if (item.date == daydata.value) {
      item.is = true;
    }
  });
};
const itemClick = (index, item) => {
  dateArr.value.forEach((item) => {
    item.is = false;
  });

  value3.value = item.date;
  getClockPoliceStatic();
  dateArr.value[index].is = true;
};

const handleClickTab = (tab) => {
  isIndex.value = tab.index;
  getClockPoliceStatic(tab.index);
};

let ycqkList = ref([]);
let jlArr = ref([]);
const getClockPoliceStatic = async () => {
  let obj = {
    page: 1,
    limit: 299,
    orgId: sessionStorage.getItem("orgIds"),
    exceptType: "",
    startDate: value3.value+' '+'00:00:00',
    endDate: value3.value+' '+'23:59:59',
    state: isIndex.value,
    reportType: "zf,jg",
    reportState: "1",
  };

  let res = await http.znjw.ExceptionReportSelect(obj);
  if (!res) return;
  if (res.code == 0) {
    ycqkList.value = res.data.data;
    ycqkList.value = sortedArray.value;
    if (isIndex.value == 1) {
      ycqkList.value = res.data.data;
      ycqkList.value = sortedArray.value;
    }
  }
};
const mergeCells = ({ row, column, rowIndex, columnIndex }) => {
  if (columnIndex === 0) {
    // 只合并第一列
    const prevRow = ycqkList.value[rowIndex - 1];
    const currRow = ycqkList.value[rowIndex];
    if (prevRow && prevRow.orgName === currRow.orgName) {
      return [1, 0]; // [rowspan, colspan]
    } else {
      const count = 1;
      let mergeCount = 1;
      for (let i = rowIndex + 1; i < ycqkList.value.length; i++) {
        if (ycqkList.value[i].orgName === currRow.orgName) {
          mergeCount++;
        } else {
          break;
        }
      }
      return [mergeCount, 1];
    }
  } else {
    return [1, 1];
  }
};


// 使用计算属性来排序数组
const sortedArray = computed(() => {
  return [...ycqkList.value].sort((a, b) => a.orgName.localeCompare(b.orgName));
});
watch(
  () => props.jqArr,
  (newValue, oldValue) => {
    if (newValue) {
      getFormattedDateTime();
      datePicker.value = [daydata.value, daydata.value];
      value3.value = daydata.value;
      getMonthDates();
      if (props.jqArr) {
        getClockPoliceStatic();
      }
    } else {
    }
  },
  {
    deep: true, // 深度监听
    immediate: true, // 立即触发回调
  }
);
</script>

<style scoped lang="scss">

/**
 标签样式修改
 */
 ::v-deep .el-tabs--card > .el-tabs__header .el-tabs__nav {
  border: 1px solid rgb(5, 112, 146) !important;
}
::v-deep .el-tabs--card > .el-tabs__header {
  border-bottom: 1px solid rgb(5, 112, 146) !important;
}
::v-deep .el-tabs--card > .el-tabs__header .el-tabs__item {
  color: white;
  border-left: 1px solid rgb(5, 112, 146) !important;
}
::v-deep .el-tabs--card > .el-tabs__header .el-tabs__item.is-active {
  border-bottom-color: rgb(0, 247, 255) !important;
  color: rgb(0, 247, 255);
}
::v-deep .el-tabs__header {
  margin: 0 0 0px;
}

::v-deep .el-tabs {
  margin-top: 10px;
}


::v-deep .date_searchbox {
  .el-input__wrapper {
    background-color: rgba(15, 82, 163, 0.278);
    box-shadow: (0 0 0 1px rgba(15, 82, 163, 0.61)) inset;
  }
  .el-input__inner {
    color: white;
  }
  .el-input__prefix {
    color: white;
  }
}

.isAtv {
  background-color: rgb(4 110 250 / 27%) !important;
  font-weight: bold;
}
.drawer_box {
  width: 100%;
  display: flex;
  padding: 0 10px;
  height: 100%;
  box-sizing: border-box;
}

::v-deep .isTable .el-table {
  background-color: rgb(8, 32, 78) !important;
  .el-table tr {
    background-color: rgb(8, 32, 78) !important;
  }
  .cell {
    height: 30px;
    color: #fff;
    text-align: center;
    // border: 1px solid #ccc;
    background: rgb(8, 32, 78) !important;
    font-size: 12px;
    line-height: 30px;
  }
  .el-table__cell {
    border-color: 1px solid rgb(5, 112, 146);
    padding: 2px 0px !important;
    background: rgb(8, 32, 78) !important;
  }
  .el-table--fit .el-table__inner-wrapper:before {
    display: none !important;
  }
  .el-table th.el-table__cell.is-leaf {
    border: none !important;
  }
  --el-table-border-color: rgb(5, 112, 146);
}
</style>
